package soccerBet.project.services;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Workbook;

import net.sf.jxls.exception.ParsePropertyException;
import net.sf.jxls.transformer.XLSTransformer;

public class XLSService {

	public XLSService() {

	}

	public void geraXls(List staff, String SIDEmail) {

		Map beans = new HashMap();
		beans.put("historico", staff);
		XLSTransformer transformer = new XLSTransformer();
		transformer.markAsFixedSizeCollection("historico");

		System.out.println(System.getProperty("user.dir"));

		File file = new File(System.getProperty("user.dir") + "\\fil");
		if (!file.exists()) {
			if (file.mkdir()) {
				System.out.println("Directory is created!");
			} else {
				System.out.println("Failed to create directory!");
			}
		}

		URL url = getClass().getResource("/files/template_historico.xls");

		String filesPathAndName = url.getPath();

		try {

			if (filesPathAndName != null) {

				InputStream is = getClass().getResourceAsStream(
						"/files/template_historico.xls");

				Workbook wb = transformer.transformXLS(is, beans);

				FileOutputStream out = new FileOutputStream(new File(
						System.getProperty("user.dir") + "\\fil" + "\\"
								+ SIDEmail + ".xlsx"));
				wb.write(out);
				out.close();
			}

		} catch (ParsePropertyException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
